<?

/***************************************************************************
 Jupiter Content System @ Jupiterportal.com
 Copyright (C) 2005 Cosmin Flavius (highstrike@gmail.com)

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 More Info About The Licence At http://www.gnu.org/copyleft/gpl.html
****************************************************************************/

//-----------------------------------------------------------------
//  Filter Acces Rightes
//-----------------------------------------------------------------

if(isset($is_guest) || isset($is_user) || isset($is_moderator))
{ header("location: $PHP_SELF?i=2"); exit; }

switch($a)
{
	//-----------------------------------------------------------------
	//  Send the mail
	//-----------------------------------------------------------------

	case 1:

		//-----------------------------------------------------------------
		//  Checks
		//-----------------------------------------------------------------

		if(!$subject_choice) { header("location: $PHP_SELF?n=modules/mass-email&i=62"); exit; }
		if(!$message_choice) { header("location: $PHP_SELF?n=modules/mass-email&i=63"); exit; }

		//-----------------------------------------------------------------
		//  Rezolve any notice
		//-----------------------------------------------------------------

		if(!isset($from)) $from = NULL;
		if(!isset($raw)) $raw = NULL;

		//-----------------------------------------------------------------
		//  Load all emails in a variable
		//-----------------------------------------------------------------

		$users = $db->getTable("SELECT email FROM users");

		foreach($users as $idx=>$user)
		{
			// For email string
			$raw .= $user['email'];
			$raw .=',';
		}

		//-----------------------------------------------------------------
		//  Configure everything and send email
		//-----------------------------------------------------------------

		$subject = stripslashes($subject_choice);
		$message = stripslashes($message_choice);

		$to = "highstrike@gmail.com";

		$from .= "MIME-Version: 1.0\r\n";
		$from .= "Content-type: text/html; charset=iso-8859-1\r\n";
		$from .= "Bcc: $raw\r\n";    
		$from .= "From: {$config['website_name']} - {$config['webmaster_name']} <'{$config['webmaster_email']}'>; \n";

		$result = mail("{$config['webmaster_email']}" , $subject, $message, $from);

		//-----------------------------------------------------------------
		//  If succesfull redirect + add log, else redirect with error
		//-----------------------------------------------------------------

		if($result)
		{
			//-----------------------------------------------------------------
			//  Add Log + Redirect
			//-----------------------------------------------------------------

			$db->insertRow("log",array('user_id' => ''.$_SESSION['id'].'','user_user' => ''.$_SESSION['username'].'','log' => 'Mass-Email used where subject -> \''.$subject.'\'','date' => ''.time().''));

			header("location: $PHP_SELF?n=modules/mass-email&a=2&i=64");
		}
		else header("location: $PHP_SELF?n=modules/mass-email&i=65");

	break;

	//-----------------------------------------------------------------
	//  Result
	//-----------------------------------------------------------------

	case 2:

		//-----------------------------------------------------------------
		//  Rezolve any notice
		//-----------------------------------------------------------------

		if(!isset($list)) $list = NULL;

		//-----------------------------------------------------------------
		//  Load all emails in a variable
		//-----------------------------------------------------------------

		$users = $db->getTable("SELECT email FROM users");

		foreach($users as $idx=>$user)
		{
			// For email string
			$list .= $user['email'];
			$list .=', ';
		}

		//-----------------------------------------------------------------
		// Echo the back link
		//-----------------------------------------------------------------

		echo "<table class='main' cellspacing='1' cellpadding='4'><tr class='head'><td colspan='3'>{$language['Admin name']}</td></tr><tr><td colspan='3' class='con1' onMouseOver=this.className='con3' onMouseOut=this.className='con1' onclick=window.location.href='$PHP_SELF?n=modules/admin'><a href='$PHP_SELF?n=modules/admin'> &raquo; {$language['Admin back']}</a></td></tr></table><br>" ?>
			<table class="main" cellspacing="1" cellpadding="4" width="100%">
			<tr class="head">
			<td class="head"><?= $language['Mass-Email result title'] ?></td>
			</tr>
			<td class="con1"><?= $list ?></td>
			</tr>
			</table>
		<?

	break;

	//-----------------------------------------------------------------
	//  View Mail form
	//-----------------------------------------------------------------

	default:

		//-----------------------------------------------------------------
		// Echo the back link
		//-----------------------------------------------------------------

		echo "<table class='main' cellspacing='1' cellpadding='4'><tr class='head'><td colspan='3'>{$language['Admin name']}</td></tr><tr><td colspan='3' class='con1' onMouseOver=this.className='con3' onMouseOut=this.className='con1' onclick=window.location.href='$PHP_SELF?n=modules/admin'><a href='$PHP_SELF?n=modules/admin'> &raquo; {$language['Admin back']}</a></td></tr></table><br>" ?>
			<form name="send" method="post" action="<?= $PHP_SELF ?>?n=modules/mass-email">
			<table class="main" cellspacing="1" cellpadding="4" width="100%">
			<tr class="head">
			<td class="head" colspan="2"><?= $language['Mass-Email form title'] ?></td>
			</tr>
			<tr>
			<td class="con1" width="100" nowrap><?= $language['Mass-Email form desc'] ?></td>
			<td class="con1"><input type="text" style="width:170" class="box" name="subject_choice"></td>
			</tr>
			<tr>
			<td class="con1" valign="top"><?= $language['Mass-Email form desc2'] ?></td>
			<td class="con1"><textarea rows="15" cols="80" class="box" name="message_choice"></textarea></td>
			</tr>
			<tr>
			<td class="con1"><?= $language['Mass-Email form desc3'] ?></td>
			<td class="con1"><input type="submit" style="width:170" class="box" value="<?= $language['Mass-Email form desc4'] ?>" name="sendmail"></td>
			</tr>
			<input type="hidden" name="a" value="1">
			</table>
			</form>
		<?
	break;
}
?>